<?php
namespace app\admin\controller;

use app\common\model\ShouyePartner as PartnerModel;
use app\common\controller\AdminBase;
use think\Db;

/**
 * 导航管理
 * Class Nav
 * @package app\admin\controller
 */
class Partner extends AdminBase
{
	protected $nav_model;

    protected function _initialize()
    {
        parent::_initialize();
        $this->partner_model  = new PartnerModel();
    }

    public function index($keyword = '',$page = 1)
    {
    	$partners = $this->partner_model->getpartnerlist($keyword,$page);
    	return $this->fetch('index',['keyword'=>$keyword,'partners'=>$partners]);
    }

    /**
     * 合作伙伴显示与否
     */
    public function changestatus($id)
    {
    	$change = $this->partner_model->change($id);
    	if($change){
    		$this->success('操作成功');
    	}else{
    		$this->error('操作失败');
    	}
    }

    /**
     * 合作伙伴的添加页面的显示与保存
     */
    public function add()
    {
        return $this->fetch('add');
    }

    public function save()
    {
        if ($this->request->isPost()) {
            $data            = $this->request->param();
            $validate_result = $this->validate($data, 'ShouyePartner');              
            if ($validate_result !== true) {
                $this->error($validate_result);
            } else {
                if ($this->partner_model->allowField(true)->save($data)) {
                    $this->success('保存成功');
                } else {
                    $this->error('保存失败');
                }
            }
        }
    }


    /**
     * 合作伙伴的编辑页面与保存
     */
    public function edit($id)
    {
        $partner = $this->partner_model->find($id);
        return $this->fetch('edit', ['partner' => $partner]);
    }

    public function update($id)
    {
        if ($this->request->isPost()) {
            $data            = $this->request->param();
            $validate_result = $this->validate($data, 'ShouyePartner');

            if ($validate_result !== true) {
                $this->error($validate_result);
            } else {
                if ($this->partner_model->allowField(true)->save($data, $id) !== false) {
                    $this->success('更新成功');
                } else {
                    $this->error('更新失败');
                }
            }
        }
    }

     /**
     * 合作伙伴的删除分类
     * @param $id
     * @throws \think\Exception
     */
    public function delete($id)
    {
        if ($this->partner_model->destroy($id) !== false) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

}